﻿//
// InventoryViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace RSD.ViewModel
{
    using Data;

    public class InventoryViewModel : ViewModelBase
    {
        private readonly IMeasuredItemRepository measuredItemRepository;
        private readonly ItemClassificationRepository itemClassificationRepository;
        private readonly RelayCommand searchCommand;

        private ItemClassification selectedItemClassification;
        private DateTime timeFrom = DateTime.Now;
        private DateTime timeTo = DateTime.Now;

        public InventoryViewModel(IMeasuredItemRepository measuredItemRepository,
            ItemClassificationRepository itemClassificationRepository)
        {
            this.measuredItemRepository = measuredItemRepository;
            this.itemClassificationRepository = itemClassificationRepository;

            this.SearchResults = new ObservableCollection<MeasuredItem>();
            this.ItemClassifications = new ObservableCollection<ItemClassification>(
                itemClassificationRepository.FindAll());

            this.searchCommand = new RelayCommand(SearchExecute);
        }

        public DateTime TimeFrom
        {
            get
            {
                return timeFrom;
            }
            set
            {
                timeFrom = value;
                RaisePropertyChanged("TimeFrom");
            }
        }

        public DateTime TimeTo
        {
            get
            {
                return timeTo;
            }
            set
            {
                timeTo = value;
                RaisePropertyChanged("TimeTo");
            }
        }

        public ItemClassification SelectedItemClassification
        {
            get
            {
                return selectedItemClassification;
            }
            set
            {
                selectedItemClassification = value;
                RaisePropertyChanged("SelectedItemClassification");
            }
        }

        public ObservableCollection<ItemClassification> ItemClassifications
        {
            get;
            set;
        }

        public ObservableCollection<MeasuredItem> SearchResults
        {
            get;
            set;
        }

        public ICommand SearchCommand
        {
            get
            {
                return searchCommand;
            }
        }

        private void SearchExecute()
        {
            var results = measuredItemRepository.Search(timeFrom, timeTo,
                SelectedItemClassification);

            SearchResults.Clear();

            foreach (var item in results)
            {
                SearchResults.Add(item);
            }
        }
    }
}
